<!DOCTYPE html>
<title>Test video looping.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<video autoplay></video>
<script>
// Test looping by:
// Play to end with "loop" set to true.
// When "seeked" event fires, verify that time has jumped back and movie is playing.
// Set "loop" to false and play again.
// Verify that "ended" event fires.
async_test(function(t) {
    var video = document.querySelector("video");
    // Test setting/removing the attribute.
    assert_equals(video.getAttribute("loop"), null);
    assert_false(video.loop);

    video.loop = true;
    assert_true(video.loop);
    assert_not_equals(video.getAttribute("loop"), null);

    video.removeAttribute("loop");
    assert_false(video.loop);

    video.onplay = t.step_func(function() {
        video.onplay = null;
        assert_false(video.paused);

        // Pause playback so the movie can't possibly loop before the seeked event fires.
        video.pause();
        // seek to near the end, wait for "seeked" event to announce loop.
        var seekCount = 0;
        video.onseeked = t.step_func(function() {
            switch (++seekCount) {
            case 1:
                // first seek completed, beginning playback.
                assert_true(video.paused);
                assert_false(video.ended);
                video.play();
                break;
            case 2:
                // second seek completed because video looped, toggle "loop" and seek to near end again.
                assert_false(video.paused);
                assert_false(video.ended);
                video.pause();
                assert_greater_than_equal(video.currentTime, 0);
                assert_less_than(video.currentTime, video.duration);
                video.loop = false;
                video.currentTime = video.duration - 0.4;
                break;
            case 3:
                // third seek completed, beginning playback for the last time.
                assert_true(video.paused);
                assert_false(video.ended);
                video.play();
                break;
            }
        });

        video.currentTime = video.duration - 0.4;
    });

    video.onended = t.step_func_done(function() {
        assert_true(video.ended);
        assert_equals(video.currentTime, video.duration);
    });

    // Set "loop" to true and begin playing.
    video.loop = true;
    video.src = "content/test.ogv";
});
</script>